/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.huawei.spare.part.replace.views.replace.perform;

import animatefx.animation.FadeInDown;
import animatefx.animation.FadeInUp;
import animatefx.animation.FadeOutUp;
import com.google.common.collect.Lists;
import com.huawei.spare.part.replace.framework.SmartContentPresenter;
import javafx.animation.PauseTransition;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.util.Duration;
import org.controlsfx.glyphfont.FontAwesome;
import org.controlsfx.glyphfont.GlyphFont;
import org.controlsfx.glyphfont.GlyphFontRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @author Woo
 */
public class PerformPresenter1 extends SmartContentPresenter {

    private static final Logger log = LoggerFactory.getLogger(SmartContentPresenter.class);

    @FXML Button performBtn;
    @FXML HBox title;
    @FXML HBox backup;
    @FXML HBox pullout;
    @FXML HBox plugin;
    @FXML HBox uplink;

    GlyphFont FA;
    List<HBox> steps;

    @Override
    public void initialize() {
        FA = GlyphFontRegistry.font("FontAwesome");
        steps = Lists.newArrayList(backup, pullout, plugin, uplink);
    }

    @FXML
    private void perform(ActionEvent event) {
        log.info("Start perform replacement workflow now");
        final FadeOutUp fadeOutUp = new FadeOutUp(performBtn);
        fadeOutUp.getTimeline().setOnFinished(e -> performBtn.setManaged(false));
        final FadeInDown titleShowUp = new FadeInDown(title);
        titleShowUp.getTimeline().setOnFinished(e -> {
            processStep(steps.remove(0));
        });

        fadeOutUp.playOnFinished(titleShowUp).play();
    }

    private void processStep(HBox box) {
        final FadeInUp showBackup = new FadeInUp(box);
        final PauseTransition delay = new PauseTransition(Duration.millis(3500));
        delay.setOnFinished(e -> {
            box.getChildren().remove(box.getChildren().size() - 1);
            box.getChildren().add(FA.create(FontAwesome.Glyph.CHECK).color(Color.web("#52c41a")).size(18D));
            if (steps.size() > 0) {
                processStep(steps.remove(0));
            }
        });

        showBackup.getTimeline().setOnFinished(e -> {
            delay.play();
        });
        showBackup.play();
    }
}
